package com.daguchuangyi.erp_android.productlist.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.daguchuangyi.erp_android.R;

/**
 * Created by lijianjian on 2018/3/17.
 */

public class CircleShadowButton extends View {
    private static final String TAG = "CircleShadowButton";
    private static final float DEFAULT_BUTTON_SIZE = 50f;
    private static final float DEFAULT_TEXT_SIZE = 36f;

    private float mButtonSize;
    private float mShadowSize;
    private int mMainColor;
    private int mShadowColor;

    private Paint mMainPaint;

    public CircleShadowButton(Context context) {
        this(context, null);
    }

    public CircleShadowButton(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleShadowButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CircleShadowButton);
        mButtonSize = ta.getDimension(R.styleable.CircleShadowButton_button_size, DEFAULT_BUTTON_SIZE);
        mShadowSize = ta.getDimension(R.styleable.CircleShadowButton_button_shadow_size, 10);
        mMainColor = ta.getColor(R.styleable.CircleShadowButton_button_main_color, Color.parseColor("#37c763"));
        mShadowColor = ta.getColor(R.styleable.CircleShadowButton_button_shadow_color, Color.parseColor("#c737c763"));

        ta.recycle();

        mMainPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mMainPaint.setColor(mMainColor);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension((int) mButtonSize, (int) (mButtonSize + mShadowSize));
    }

    @Override
    protected void onDraw(Canvas canvas) {

        Bitmap bitmap = generateBallBitmap(getWidth() / 2, (int) (mShadowSize / 2));
        canvas.drawBitmap(bitmap, 0, 0, null);

    }


    private Bitmap generateBallBitmap(int ballSize, int ballShadowSize) {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(mMainColor);
        paint.setShadowLayer(ballShadowSize, 0, ballShadowSize, mShadowColor);
        Bitmap bitmap = Bitmap.createBitmap((int) ((ballSize + ballShadowSize) * 2f),
                (int) ((ballSize + ballShadowSize) * 2f) + ballShadowSize, Bitmap.Config.ARGB_8888);
        bitmap.eraseColor(Color.TRANSPARENT);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawCircle(ballSize, ballSize, ballSize, paint);
        return bitmap;
    }

}
